package com.crazymo.messenger;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import com.crazymo.messenger.aidl.MyMessengerService;
import com.crazymo.messenger.rawmessenger.MessengerService;

public class MainActivity extends AppCompatActivity {
    private final static String TAG="MainActivity";
    /***********************1、Messenger 方式****************************/
    private Messenger mServer;
    private ServiceConnection connMessenger =new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mServer=new Messenger(service);//把返回的IBinder对象初始化Messenger
            Log.e(MessengerService.TAG, "MessengerService Connected!");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    private final Handler handlerClient =new Handler(){
        @SuppressLint("HandlerLeak")
        @Override
        public void handleMessage(Message msg) {
            if(msg!=null && msg.what== MessengerService.MSG_WHAT_RESP){
                String resp=msg.getData().getString(MessengerService.KEY_RESP);
                Log.e(MessengerService.TAG, "resp from server="+resp);
            }
        }
    };

    //为了接收服务端的回复，客户端也需要准备一个接收消息的Messenger 和Handler
    private final Messenger clientMessenger=new Messenger(handlerClient);

    private void bindMessengerService() {
        Intent intent=new Intent(this,MessengerService.class);
        bindService(intent, connMessenger, Context.BIND_AUTO_CREATE);

    }

    public void sendByMessenger(View view) {
        Message msg=Message.obtain(null,MessengerService.MSG_WHAT_HELLO);
        Bundle data=new Bundle();
        data.putString(MessengerService.KEY_NAME,"CrazyMo_");
        msg.setData(data);
        //Client 发信时指定希望回信人，把客户端进程的Messenger对象设置到Message中
        msg.replyTo=clientMessenger;
        try {
            mServer.send(msg);//跨进程传递
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /***********************2、MyMessenger AIDL方式****************************/
    private IMyMessenger myInterface;
    private ServiceConnection connAidl = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myInterface = IMyMessenger.Stub.asInterface(service);
            Log.i(TAG, "MyMessenger 连接Service 成功");
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e(TAG, "连接Service失败");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bindMessengerService();
        bindMyMessengerService();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(connMessenger);
        unbindService(connAidl);
    }

    public void sendByMyMessenger(View view) {
        try {
            String ret=myInterface.send("hello server my MyMessenger");
            Log.i(TAG, "myInterface.send的结果="+ret);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private void bindMyMessengerService() {
        Intent intent=new Intent(this, MyMessengerService.class);
        bindService(intent, connAidl, Context.BIND_AUTO_CREATE);
    }
}